<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../libs/vue.js"></script>
    <style>
        .btn-list {
            display: flex;
        }

        .btn {
            width: 100px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            border: 1px solid #000;
            cursor: pointer;
        }

        .content {
            width: 300px;
            height: 200px;
            border: 5px dashed red;
        }

        .active {
            background-color: pink;
            color: #fff;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="btn-list">
            <div class="btn" :class="page=='home'?'active':''" @click="toggle('home')">首页</div>
            <div class="btn" :class="page=='news'?'active':''" @click="toggle('news')">新闻</div>
            <div class="btn" :class="page=='about'?'active':''" @click="toggle('about')">关于</div>
        </div>
        <div class="content">
            <!-- 缓存组件 -->
            <keep-alive>
                <!-- 动态组件 -->
                <component :is="page"></component>
            </keep-alive>
        </div>

    </div>
    <script>
        let home = {
            template: `
                <h1>我是首页</h1>
            `,
            created() {
                console.log('首页被创建')
            },
            activated() {
                console.log('首页被激活')
            },
            deactivated() {
                console.log('首页被失活')
            },
        }
        let news = {
            template: `
                <h1>我是新闻页</h1>
            `,
            created() {
                console.log('新闻被创建')
            },
            activated() {
                console.log('新闻被激活')
            },
            deactivated() {
                console.log('新闻被失活')
            },
        }
        let about = {
            template: `
                <h1>我是关于页</h1>
            `,
            created() {
                console.log('关于被创建')
            },
            activated() {
                console.log('关于被激活')
            },
            deactivated() {
                console.log('关于被失活')
            },
        }
        let vm = new Vue({
            el: '#app',
            data: {
                page: 'home'
            },
            methods: {
                toggle(page) {
                    this.page = page
                }
            },
            components: {
                home, news, about
            }
        })
    </script>
</body>

</html>